
/*
 * ResponseScreen.java
 *
 * Created on Oct 18, 2009, 11:22:00 AM
 */
package sg.edu.nus.iss.ooad.jass.ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import sg.edu.nus.iss.ooad.jass.controller.JobAdvertisementController;
import sg.edu.nus.iss.ooad.jass.controller.ResponseController;
import sg.edu.nus.iss.ooad.jass.entity.JobAdvertisement;
import sg.edu.nus.iss.ooad.jass.entity.LoginInfo;
import sg.edu.nus.iss.ooad.jass.entity.Response;
import sg.edu.nus.iss.ooad.jass.ui.widget.AbstractJASSRegularWindow;

/**
 *
 * @author Administrator
 */
public class JASSResponseConfirmation extends AbstractJASSRegularWindow {

        private static final long serialVersionUID = 3589719073438612716L;
        private static final Logger logger = Logger.getLogger(JASSResponseConfirmation.class.getName());
        // Variables declaration - do not modify
        private java.awt.Button btnCancel;
        private java.awt.Button btnConfirm;
        private java.awt.Label label1;
        private java.awt.Label label2;
        private java.awt.Label label4;
        private java.awt.Label label7;
        private java.awt.Label label8;
        private java.awt.Label labelCompany;
        private java.awt.Label labelJobSeeker;
        private java.awt.Label labelJobTitle;
        private java.awt.Label labelStatus;
        private java.awt.Panel inputPanel;
        private GridBagLayout inputLayout = null;
        // End of variables declaration 
        private AbstractJASSRegularWindow parent;
        private Response response;

        /** 
         * Creates new form ResponseScreen
         */
        public JASSResponseConfirmation(AbstractJASSRegularWindow parent ) {
                super("JASS Response Job Advertisement", new Dimension(550, 400));
                initComponents();
                this.parent= parent;
        }
        
    /**
     * Set response object
     * @param response
     */
        public void setResponse(Response response) {
                this.response = response;
        }

        /**
         * This method is called from within the constructor to initialize the form.
         */
        private void initComponents() {
                Panel titlePanel = new Panel();
                titlePanel.setLayout(new BorderLayout(0, 0));
                titlePanel.setPreferredSize(new Dimension(550, 20));
                Panel buttonPanel = new Panel();

                inputPanel = new Panel();
                inputLayout = new GridBagLayout();
                inputPanel.setLayout(inputLayout);
                label1 = new java.awt.Label();
                label2 = new java.awt.Label();
                labelJobTitle = new java.awt.Label();
                label4 = new java.awt.Label();
                labelCompany = new java.awt.Label();
                labelJobSeeker = new java.awt.Label();
                label7 = new java.awt.Label();
                label8 = new java.awt.Label();
                labelStatus = new java.awt.Label();
                btnConfirm = new java.awt.Button();
                btnCancel = new java.awt.Button();

                buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));


                addWindowListener(new java.awt.event.WindowAdapter() {

                        @Override
                        public void windowClosing(java.awt.event.WindowEvent evt) {
                                exitForm(evt);
                        }
                });

                label1.setFont(new java.awt.Font("Dialog", 1, 12));
                label1.setText("Response Details");
                titlePanel.add(label1);

                label2.setText("Job Advertisement Title");
                label4.setText("Job  Company");
                label7.setText("Job Seeker");
                label8.setText("Response Status");

                attachComponent(label2, 0, 2, 1, 1);
                attachComponent(labelJobTitle, 1, 2, 1, 1);
                attachComponent(label4, 0, 3, 1, 1);
                attachComponent(labelCompany, 1, 3, 1, 1);
                attachComponent(label7, 0, 4, 1, 1);
                attachComponent(labelJobSeeker, 1, 4, 1, 1);
                attachComponent(label8, 0, 5, 1, 1);
                attachComponent(labelStatus, 1, 5, 1, 1);


                btnConfirm.setLabel("Confirmation");
                buttonPanel.add(btnConfirm);
                btnConfirm.addActionListener(new ApplyJobActionListener());



                btnCancel.setLabel("Cancel");
                buttonPanel.add(btnCancel);
                btnCancel.addActionListener(new exitActionListener());

                setLayout(new BorderLayout(5, 5));
                add(titlePanel, BorderLayout.NORTH);
                add(inputPanel, BorderLayout.CENTER);
                add(buttonPanel, BorderLayout.SOUTH);
                pack();
        }

        /** 
         * Exit the Application
         */
        private void exitForm(java.awt.event.WindowEvent evt) {
                System.exit(0);
        }

      /**
       * Display the form fields
       */
        public void display() {
                JobAdvertisementController jobAdvertisemnetController = new JobAdvertisementController();
                try {
                        JobAdvertisement job = jobAdvertisemnetController.retrieveJobAdvertisement(response.getJobAdvertisementID());
                        labelJobTitle.setText(job.getJobTitle());
                        labelCompany.setText(job.getCompanyDepartment());
                } catch (Exception e) {
                        logger.log(Level.SEVERE, null, e);
                }

                labelJobSeeker.setText(LoginInfo.getLoginId());
                labelStatus.setText(transformStatus(response.getResponseStatus()));


        }

        /**
         * Attach the component 
         * @param c
         * @param gridx
         * @param gridy
         * @param gridwidth
         * @param gridheight
         * @param fill
         */
        @Override
        public void attachComponent(Component c, int gridx, int gridy, int gridwidth, int gridheight, int fill) {
                GridBagConstraints constraint = new GridBagConstraints();
                constraint.gridx = gridx;
                constraint.gridy = gridy;
                constraint.gridwidth = gridwidth;
                constraint.gridheight = gridheight;
                constraint.fill = fill;
                constraint.insets = new Insets(4, 4, 4, 4);
                constraint.weightx = 1.0;
                constraint.weighty = 1.0;
                inputPanel.add(c);
                inputLayout.setConstraints(c, constraint);
        }

        /**
         * Transform the response status 
         * @param status
         * @return
         */

        private String transformStatus(String status) {
                if (status.equals("D")) {
                        return "DRAFT";
                } else if (status.equals("A")) {
                        return "ACCEPTED";
                } else if (status.equals("I")) {
                        return "INTERVIEWED";
                } else if (status.equals("R")) {
                        return "REJECTED";
                } else {
                        return "";
                }
        }

        /**
         * Exit from the form.
         */
        class exitActionListener implements ActionListener {

                public void actionPerformed(ActionEvent e) {
                        close();
                }
        }

        /**
         * Apply job action
         */

        class ApplyJobActionListener implements ActionListener {

                public void actionPerformed(ActionEvent e) {
                        boolean success = CreateResponse(); 
                        if (success) {
                               displayMessage("JASS Response Job Advertisement", "Apply successfully");
                               close(); 
                        } else {
                               displayMessage("JASS Response Job Advertisement", "Fail to apply");
                        }
                }
        }

/**
 * Create job the response entry 
 * @return
 */

        private boolean CreateResponse() {
                ResponseController responsecontroller = new ResponseController();
                boolean success = false;
                try {
                        success = responsecontroller.createResponse(response);

                } catch (Exception e) {
                        logger.log(Level.SEVERE, null, e);

                }
                return success;
        }

      
/**
 *  Reset the form
 */
        @Override
        public void resetForm() {
                throw new UnsupportedOperationException("Not supported yet.");
        }
}
